#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("事件过滤测试");

    //给控件安装事件过滤，为的是与eventFilter产生关联 (类似于connect)
    ui->label->installEventFilter(this);
    ui->label_2->installEventFilter(this);
    ui->label_3->installEventFilter(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


//watched 表示是由哪一个控件触发的，event表示发生了什么事件
 bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->label)
    {
        int x = ui->label->x(), y = ui->label->y();
        int width = ui->label->width(), height = ui->label->height();

        if(event->type() == QEvent::MouseButtonPress)
        {
//             qDebug() << "鼠标点击事件1";
//             QMatrix matrix;
//             matrix.scale(2,2);
//             QPixmap tempImage = ui->label->pixmap()->transformed(matrix);
//             ui->label->setPixmap(tempImage);
             ui->label->setGeometry(x, y, width * 2, height * 2);

             ui->label_4->setText("鼠标点击事件1");
        }

        if(event->type() == QEvent::MouseButtonRelease)
        {
            qDebug() << "鼠标释放事件";
            ui->label->setGeometry(x, y, width, height);
        }

    }
    else if(watched == ui->label_2)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
             qDebug() << "鼠标点击事件2";
             QMatrix matrix;
             matrix.scale(2,2);
             QPixmap tempImage = ui->label_2->pixmap()->transformed(matrix);
             ui->label_2->setPixmap(tempImage);

             ui->label_4->setText("鼠标点击事件2");
        }
    }
    else if(watched == ui->label_3)
    {
        if(event->type() == QEvent::MouseButtonPress)
        {
             qDebug() << "鼠标点击事件3";
             QMatrix matrix;
             matrix.scale(2,2);
             QPixmap tempImage = ui->label_3->pixmap()->transformed(matrix);
             ui->label_3->setPixmap(tempImage);

             ui->label_4->setText("鼠标点击事件3");
        }
    }

    //return MainWindow::eventFilter(watched, event);
    //return true;
}





